package com.fingard.dsp.bank.directbank.cncbi01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IForeignCurrency;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqQryForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.RetAtsForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.detail.RetAtsForeignCurrPayDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/11/14.
 */
public class CNCBI01ForeignCurrSinglePay extends CNCBI01Base implements IForeignCurrency {
    @Override
    public RetAtsForeignCurrSingle foreignCurrPay(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetDetail = null;
        try {
            if ("OutsideFCP".equals(reqForeignCurrSingle.reqHeader.transType)) {
                if ("190801".equals(reqForeignCurrSingle.reqHeader.commandCode)) {
                    tmpRetDetail = DLINETRN(reqForeignCurrSingle);
                } else {
                    tmpRetDetail = DLCBITRN(reqForeignCurrSingle);
                }
            } else {
                tmpRetDetail = new RetAtsForeignCurrPayDetail();
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未知的业务类型：" + reqForeignCurrSingle.reqHeader.transType;
            }
        } catch (Exception ex) {
            tmpRetDetail = new RetAtsForeignCurrPayDetail();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    private RetAtsForeignCurrPayDetail DLCBITRN(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrPayDetail detail = new RetAtsForeignCurrPayDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        XElement stream = new XElement("stream", "");
        stream.addChild(new XElement("action", "DLCBITRN"));
//        <userName></userName><!--登录名 varchar(30)-->
        stream.addChild(new XElement("userName", bankFront.loginName));
//        <clientID></clientID><!--客户流水号 varchar (20)-->
        stream.addChild(new XElement("clientID", reqForeignCurrSingle.singleDetail.askNumber));
        if (StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.bookDate) || DateHelper.compareDay(new Date(), Format.Date10Format.parse(reqForeignCurrSingle.singleDetail.bookDate)) >= 0) {
            stream.addChild(new XElement("preFlg", "0"));
            stream.addChild(new XElement("preDate", ""));
            stream.addChild(new XElement("preTime", ""));
        } else {
//            <preFlg></preFlg><!--预约支付标志char(1)，0：非预约交易；1：预约交易-->
            stream.addChild(new XElement("preFlg", "1"));
//            <preDate></preDate><!--延期支付日期char(8),格式：YYYYMMDD，预约时非空-->
            stream.addChild(new XElement("preDate", reqForeignCurrSingle.singleDetail.bookDate.replace("-", "")));
//            <preTime></preTime><!--延期支付时间char(6),格式：hhmmss，只限100000，120000，140000，160000四个时间点，预约时非空；-->
            stream.addChild(new XElement("preTime", "100000"));
        }

//        <tranType></tranType><!--交易方式char(1)，1-行内转账；2-即日支付；3-海外汇款；4-普通支付-->
        String tranType;
        //收款行国家代码是否是HK
        if ("HK".equals(reqForeignCurrSingle.singleDetail.recCountryCode)) {
            //收款行是否是信银香港
            if ("KWHKHK".equals(reqForeignCurrSingle.singleDetail.recSWIFTCODE.substring(0, 6))) {
                tranType = "1";
            } else {
                //交易币种是不是港币，人民币，美元，欧元
                if ("1".equals(reqForeignCurrSingle.singleDetail.payCur) || "2".equals(reqForeignCurrSingle.singleDetail.payCur) || "4".equals(reqForeignCurrSingle.singleDetail.payCur) || "5".equals(reqForeignCurrSingle.singleDetail.payCur)) {
                    //是否加急
                    if ("1".equals(reqForeignCurrSingle.singleDetail.paySpd)) {
                        tranType = "2";
                    } else {
                        //币种是否是港币或者人民币
                        if ("1".equals(reqForeignCurrSingle.singleDetail.payCur) || "4".equals(reqForeignCurrSingle.singleDetail.payCur)) {
                            tranType = "4";
                        } else {
                            tranType = "2";
                        }
                    }
                } else {
                    tranType = "3";
                }
            }
        } else {
            //收方国家代码是否是C
            if ("C".equals(reqForeignCurrSingle.singleDetail.recCountryCode) || "CN".equals(reqForeignCurrSingle.singleDetail.recCountryCode)) {
                //收款行国家代码是否是深圳且币种是港币
                if ("4403".equals(reqForeignCurrSingle.singleDetail.recAreaCode) && "4".equals(reqForeignCurrSingle.singleDetail.payCur)) {
                    tranType = "2";
                } else {
                    //交易币种是否是人民币
                    if ("1".equals(reqForeignCurrSingle.singleDetail.payCur)) {
                        //人民币海外
                        tranType = "3";
                    } else {
                        tranType = "3";
                    }
                }
            } else {
                tranType = "3";
            }
        }
        stream.addChild(new XElement("tranType", tranType));
//        <accountNo></accountNo><!--付款账号char(34) -->
        stream.addChild(new XElement("accountNo", reqForeignCurrSingle.singleDetail.payAct));
//        <payCryType></payCryType><!--付款账户币种char(2) -->
        stream.addChild(new XElement("payCryType", convertCurToBank(reqForeignCurrSingle.singleDetail.payAct.substring(reqForeignCurrSingle.singleDetail.payAct.length() - 2))));
//        <rcvAcc></rcvAcc><!--收款账号char(34), 交易方式为行内转账时，需校验长度是12位-->
        stream.addChild(new XElement("rcvAcc", reqForeignCurrSingle.singleDetail.recAct.trim()));
        String[] tmpRecName = cutFieldValue(reqForeignCurrSingle.singleDetail.recName, 35, getCharset());
        if (tmpRecName[1].length() > 0) {
            //        <rcvNm></rcvNm><!--收款人账户名称char(35), 交易方式为行内转账时，送空；交易方式非行内转账时，必输 -->
            if ("1".equals(tranType)) {
                stream.addChild(new XElement("rcvNm", ""));
            } else {
                stream.addChild(new XElement("rcvNm", tmpRecName[0]));
            }
            //        <rcvAddress></rcvAddress><!--收款人地址char(70)，交易方式为普通支付、海外汇款和即日支付时，选输；交易方式为行内转账时，送空；交易方式为海外汇款及收款人国家代号为(CA)加拿大的海外汇款必须提供收款地址-->
            if ("1".equals(tranType)) {
                stream.addChild(new XElement("rcvAddress", ""));
            } else {
                stream.addChild(new XElement("rcvAddress", tmpRecName[1] + " " + reqForeignCurrSingle.singleDetail.recAddr));
            }
        } else {
//        <rcvNm></rcvNm><!--收款人账户名称char(35), 交易方式为行内转账时，送空；交易方式非行内转账时，必输 -->
            if ("1".equals(tranType)) {
                stream.addChild(new XElement("rcvNm", ""));
            } else {
                stream.addChild(new XElement("rcvNm", reqForeignCurrSingle.singleDetail.recName.trim()));
            }
            //        <rcvAddress></rcvAddress><!--收款人地址char(70)，交易方式为普通支付、海外汇款和即日支付时，选输；交易方式为行内转账时，送空；交易方式为海外汇款及收款人国家代号为(CA)加拿大的海外汇款必须提供收款地址-->
            if ("1".equals(tranType)) {
                stream.addChild(new XElement("rcvAddress", ""));
            } else {
                stream.addChild(new XElement("rcvAddress", reqForeignCurrSingle.singleDetail.recAddr));
            }
        }
//        <rcvCryType></rcvCryType><!--收款账户币种char(2) ,交易方式为行内转账时，必输；交易方式为即日支付和海外汇款和普通支付时，送空；-->
        if ("1".equals(tranType)) {
            if (reqForeignCurrSingle.singleDetail.recAct.length() > 2) {
                stream.addChild(new XElement("rcvCryType", convertCurToBank(reqForeignCurrSingle.singleDetail.recAct.substring(reqForeignCurrSingle.singleDetail.recAct.length() - 2))));
            } else {
                stream.addChild(new XElement("rcvCryType", ""));
            }
        } else {
            stream.addChild(new XElement("rcvCryType", ""));
        }
//        <rcvBankNo></rcvBankNo><!--收款账号行号char(10), 交易方式为行内转时，必输；交易方式为即日支付和海外汇款和普通支付时，送空；交易方式为行内转账时，数据字典：CNCBIHK-信银国际香港、CNCBISG-信银国际新加坡 -->
        if ("1".equals(tranType)) {
            //信银香港
            if ("KWHKHK".equals(reqForeignCurrSingle.singleDetail.recSWIFTCODE.substring(0, 6))) {
                stream.addChild(new XElement("rcvBankNo", "CNCBIHK"));
            } else {
                stream.addChild(new XElement("rcvBankNo", "CNCBISG"));
            }
        } else {
            stream.addChild(new XElement("rcvBankNo", ""));
        }
//        <tranAmt></tranAmt><!--交易金额 decimal（15,2）-->
        stream.addChild(new XElement("tranAmt", reqForeignCurrSingle.singleDetail.payAmount));
//        <cryType></cryType><!--交易币种char(2),
        stream.addChild(new XElement("cryType", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));
//        <feeType></feeType><!--费用扣除方式 char(3), 交易方式为行内转账和普通支付时，送空；交易方式为即日支付和海外汇款时，必输；数据字典：OUR-付款人支付、BEN-收款人支付、SHA-对半分担-->
        if ("1".equals(tranType) || "4".equals(tranType)) {
            stream.addChild(new XElement("feeType", ""));
        } else {
            stream.addChild(new XElement("feeType", reqForeignCurrSingle.singleDetail.costMode));
        }
//        <rem></rem><!--客户摘要char(102),可空-->
        stream.addChild(new XElement("rem", reqForeignCurrSingle.singleDetail.abstractATS));
//        <excratecNo></excratecNo><!--汇率合约号char(16),可空-->
        stream.addChild(new XElement("excratecNo", ""));
//        <rCountryCode></rCountryCode><!--收款人国家代号char(35), 交易方式为行内转账时，送空；交易方式为即日支付时，至少必需填写收款人国家代号，只限于HK香港，CN中国深圳；交易方式为海外汇款时，必输并且必须在指定的范围之内；如果收款人国家代号为加拿大时，收款人地址不能为空。交易方式为普通转账时，收款人国家代号必输，只限于HK香港-->
        if ("1".equals(tranType)) {
            stream.addChild(new XElement("rCountryCode", ""));
        } else {
            stream.addChild(new XElement("rCountryCode", reqForeignCurrSingle.singleDetail.recCtryCode));
        }
//        <rcvEmail></rcvEmail><!--收款人电子邮件char(200), 交易方式为行内转账时，送空；交易方式为普通支付、即日支付和海外汇款时，选输-->
        stream.addChild(new XElement("rcvEmail", ""));
//        <rbankCode></rbankCode><!--收款人银行代号char(12), 交易方式为行内转账时，送空；交易方式为海外汇款时，选输，银行的SWIFT代号; 交易方式为即日支付时，必输，根据银行的SWIFT代号；交易方式为普通支付时，必输，根据香港清算局本地银行代码表HKICL Bank Code; 交易方式为海外汇款时，收款人银行代号或收款人银行名称+收款人银行地址+收款人银行国家代号，必输其一组-->
        if ("1".equals(tranType)) {
            stream.addChild(new XElement("rbankCode", ""));
        } else if ("2".equals(tranType) || "3".equals(tranType)) {
            stream.addChild(new XElement("rbankCode", reqForeignCurrSingle.singleDetail.recSWIFTCODE));
        } else {
            // TODO: 2018/11/20  
        }

        String[] tmpBnkName = cutFieldValue(reqForeignCurrSingle.singleDetail.recBnkName, 35, getCharset());
        if (tmpBnkName[1].length() > 0) {
            //        <rcvBankNm></rcvBankNm><!--收款人银行名称char(35), 交易方式为行内转账、普通支付时，送空；交易方式为即日支付和海外汇款时，选输; 交易方式为海外汇款时，收款人银行代号或收款人银行名称+收款人银行地址+收款人银行国家代号，必输其一组-->
            if (("2".equals(tranType) || "3".equals(tranType)) && StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.recSWIFTCODE)) {
                stream.addChild(new XElement("rcvBankNm", tmpBnkName[0]));
            } else {
                stream.addChild(new XElement("rcvBankNm", ""));
            }
//        <rcvBranchNo></rcvBranchNo><!--收款人银行分支行代号char(12), 交易方式为行内转账、即日支付和海外汇款时，送空；交易方式为普通支付时，选输-->
            stream.addChild(new XElement("rcvBranchNo", ""));
//        <rBankAdd></rBankAdd><!--收款人银行地址char(70), 交易方式为行内转账、普通支付时，送空；交易方式为海外汇款、即日支付时，选输; 交易方式为海外汇款时，收款人银行代号或收款人银行名称+收款人银行地址+收款人银行国家代号，必输其一组-->
            if (("2".equals(tranType) || "3".equals(tranType)) && StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.recSWIFTCODE)) {
                stream.addChild(new XElement("rBankAdd", tmpBnkName[1] + " " + reqForeignCurrSingle.singleDetail.recBnkAddr));
            } else {
                stream.addChild(new XElement("rBankAdd", ""));
            }
        } else {
//        <rcvBankNm></rcvBankNm><!--收款人银行名称char(35), 交易方式为行内转账、普通支付时，送空；交易方式为即日支付和海外汇款时，选输; 交易方式为海外汇款时，收款人银行代号或收款人银行名称+收款人银行地址+收款人银行国家代号，必输其一组-->
            if (("2".equals(tranType) || "3".equals(tranType)) && StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.recSWIFTCODE)) {
                stream.addChild(new XElement("rcvBankNm", reqForeignCurrSingle.singleDetail.recBnkName));
            } else {
                stream.addChild(new XElement("rcvBankNm", ""));
            }
//        <rcvBranchNo></rcvBranchNo><!--收款人银行分支行代号char(12), 交易方式为行内转账、即日支付和海外汇款时，送空；交易方式为普通支付时，选输-->
            stream.addChild(new XElement("rcvBranchNo", ""));
//        <rBankAdd></rBankAdd><!--收款人银行地址char(70), 交易方式为行内转账、普通支付时，送空；交易方式为海外汇款、即日支付时，选输; 交易方式为海外汇款时，收款人银行代号或收款人银行名称+收款人银行地址+收款人银行国家代号，必输其一组-->
            if (("2".equals(tranType) || "3".equals(tranType)) && StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.recSWIFTCODE)) {
                stream.addChild(new XElement("rBankAdd", reqForeignCurrSingle.singleDetail.recBnkAddr));
            } else {
                stream.addChild(new XElement("rBankAdd", ""));
            }
        }
//        <rcvBankCCode></rcvBankCCode><!--收款人银行国家代号char(35), 交易方式为行内转账时，送空; 交易方式为即日支付、普通支付时，必输；交易方式为即日支付时，收款人银行国家代
        if ("2".equals(tranType)) {
            if ("C".equals(reqForeignCurrSingle.singleDetail.recCountryCode)) {
                stream.addChild(new XElement("rcvBankCCode", "CN"));
            } else {
                stream.addChild(new XElement("rcvBankCCode", reqForeignCurrSingle.singleDetail.recCountryCode));
            }
        } else if ("4".equals(tranType)) {
            stream.addChild(new XElement("rcvBankCCode", "HK"));
        } else if ("3".equals(tranType)) {
            stream.addChild(new XElement("rcvBankCCode", reqForeignCurrSingle.singleDetail.recCountryCode));
        } else {
            stream.addChild(new XElement("rcvBankCCode", ""));
        }
//        <postscript></postscript><!--附言char(140), 交易方式为行内转账时，送空；交易方式为普通支付、即日支付和海外汇款时，选输; 交易方式为海外汇款时，所有从境外支付到中国内地的人民币海外汇款必须提供支付用途说明（CCTFDR-资本项下跨境支付、CCDNDR-慈善捐款、CGODDR-货物贸易、COCADR-其他经常项目、CSTRDR-服务贸易）-->
//        if ("1".equals(tranType)) {
//            stream.addChild(new XElement("postscript", ""));
//        } else {
        stream.addChild(new XElement("postscript", reqForeignCurrSingle.singleDetail.purpose));
//        }
//        <payUseNo></payUseNo><!--支付用途代号char(2), 交易方式为行内转账、即日支付和海外汇款时，送空；交易方式为普通支付时，必输; 交易方式为普通支付时，数据字典：20：Sundry Credit、22：Salary Credit、23：Dividend Credit-->
        if ("4".equals(tranType)) {
            stream.addChild(new XElement("payUseNo", reqForeignCurrSingle.singleDetail.purpose));
        } else {
            stream.addChild(new XElement("payUseNo", ""));
        }

        if ("3".equals(tranType)) {
//        <interBankCode></interBankCode><!--中介银行代号char(12), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
            stream.addChild(new XElement("interBankCode", reqForeignCurrSingle.singleDetail.recAgentBnkSWIFT));
            String[] tmpAgentBnkName = cutFieldValue(reqForeignCurrSingle.singleDetail.recAgentBnkName, 35, getCharset());
            if (tmpAgentBnkName[1].length() > 0) {
                //        <interBankNm></interBankNm><!--中介银行名称char(35), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
                stream.addChild(new XElement("interBankNm", tmpAgentBnkName[0]));
//        <interBank></interBank><!--中介银行地址char(70)，交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输；-->
                stream.addChild(new XElement("interBank", tmpAgentBnkName[1] + " " + reqForeignCurrSingle.singleDetail.recAgentBnkAddr));
            } else {
                //        <interBankNm></interBankNm><!--中介银行名称char(35), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
                stream.addChild(new XElement("interBankNm", reqForeignCurrSingle.singleDetail.recAgentBnkName));
//        <interBank></interBank><!--中介银行地址char(70)，交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输；-->
                stream.addChild(new XElement("interBank", reqForeignCurrSingle.singleDetail.recAgentBnkAddr));
            }
//        <inteCCode></inteCCode><!--中介银行国家代号char(35), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
            stream.addChild(new XElement("inteCCode", reqForeignCurrSingle.singleDetail.recAgentBnkCtryCode));
        } else {
//        <interBankCode></interBankCode><!--中介银行代号char(12), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
            stream.addChild(new XElement("interBankCode", ""));
//        <interBankNm></interBankNm><!--中介银行名称char(35), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
            stream.addChild(new XElement("interBankNm", ""));
//        <interBank></interBank><!--中介银行地址char(70)，交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输；-->
            stream.addChild(new XElement("interBank", ""));
//        <inteCCode></inteCCode><!--中介银行国家代号char(35), 交易方式为行内转账、即日支付和普通支付时，送空；交易方式为海外汇款时，选输; -->
            stream.addChild(new XElement("inteCCode", ""));
        }
        nodeAnalysis(reqForeignCurrSingle.singleDetail.srcXElement, stream, getCharset());
        xmlWriter.writeXElement(stream);
        String[] bankResult = sendToBank(xmlWriter.toXmlString());
        if (bankResult[0].length() == 0) {
            StringBuilder tmpBkMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpBkMsg);
            if (tmpBkReader != null) {
                detail.respCode = "0";
                detail.respInfo = tmpBkReader.getFirstText("/stream/statusText");
                detail.transState = tmpBkReader.getFirstText("/stream/status");
                detail.payInfoCode = detail.transState;
                detail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpBkMsg.toString();
            }
        } else if (bankResult[0].equals(ConnState.FAIL)) {
            detail.respCode = "0";
            detail.respInfo = bankResult[1];
            detail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
            detail.transState = PaymentState.FAIL_Payment;
            detail.payInfoCode = bankResult[0];
        } else {
            detail.respCode = "-1";
            detail.respInfo = bankResult[1];
        }
        return detail;
    }

    private RetAtsForeignCurrPayDetail DLINETRN(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrPayDetail detail = new RetAtsForeignCurrPayDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        XElement stream = new XElement("stream", "");
        stream.addChild(new XElement("action", "DLINETRN"));
        stream.addChild(new XElement("userName", bankFront.loginName));
//        <clientID></clientID><!--客户流水号 char(20)--
        stream.addChild(new XElement("clientID", reqForeignCurrSingle.singleDetail.askNumber));
        if (StringHelper.isNullOrEmpty(reqForeignCurrSingle.singleDetail.bookDate) || DateHelper.compareDay(new Date(), Format.Date10Format.parse(reqForeignCurrSingle.singleDetail.bookDate)) >= 0) {
            stream.addChild(new XElement("preFlg", "0"));
            stream.addChild(new XElement("preDate", ""));
            stream.addChild(new XElement("preTime", ""));
        } else {
//            <preFlg></preFlg><!--预约支付标志char(1)，0：非预约交易；1：预约交易-->
            stream.addChild(new XElement("preFlg", "1"));
//            <preDate></preDate><!--延期支付日期char(8),格式：YYYYMMDD，预约时非空-->
            stream.addChild(new XElement("preDate", reqForeignCurrSingle.singleDetail.bookDate.replace("-", "")));
//            <preTime></preTime><!--延期支付时间char(6),格式：hhmmss，只限100000，120000，140000，160000四个时间点，预约时非空；-->
            stream.addChild(new XElement("preTime", "100000"));
        }
//        <payAccountNo></payAccountNo><!--付款账号 char(19)-->
        stream.addChild(new XElement("payAccountNo", reqForeignCurrSingle.singleDetail.payAct));
//        <recAccountNo></recAccountNo><!--收款账号char(19)-->
        stream.addChild(new XElement("recAccountNo", reqForeignCurrSingle.singleDetail.recAct));
//        <tranAmount></tranAmount><!--金额 decimal(15,2)-->
        stream.addChild(new XElement("tranAmount", reqForeignCurrSingle.singleDetail.payAmount));
//        <abstract></abstract><!--摘要，可空 varchar(102)-->
        stream.addChild(new XElement("abstract", reqForeignCurrSingle.singleDetail.purpose));
//        <memo></memo><!--备注 varchar(60) 可空-->
        stream.addChild(new XElement("memo", reqForeignCurrSingle.singleDetail.purpose));
//        <cryType></cryType><!--币种char(2)，可空，当收、付款账号为信银国际账号时非空-->
        stream.addChild(new XElement("cryType", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur)));
        nodeAnalysis(reqForeignCurrSingle.singleDetail.srcXElement, stream, getCharset());
        xmlWriter.writeXElement(stream);
        String[] bankResult = sendToBank(xmlWriter.toXmlString());
        if (bankResult[0].length() == 0) {
            StringBuilder tmpBkMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpBkMsg);
            if (tmpBkReader != null) {
                detail.respCode = "0";
                detail.respInfo = tmpBkReader.getFirstText("/stream/statusText");
                detail.transState = tmpBkReader.getFirstText("/stream/status");
                detail.payInfoCode = detail.transState;
                detail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpBkMsg.toString();
            }
        } else if (bankResult[0].equals(ConnState.FAIL)) {
            detail.respCode = "0";
            detail.respInfo = bankResult[1];
            detail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
            detail.transState = PaymentState.FAIL_Payment;
            detail.payInfoCode = bankResult[0];
        } else {
            detail.respCode = "-1";
            detail.respInfo = bankResult[1];
        }
        return detail;
    }

    @Override
    public RetAtsForeignCurrSingle qryForeignCurrPay(ReqQryForeignCurrSingle reqQryForeignCurrSingle) throws Exception {
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqQryForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail detail = new RetAtsForeignCurrPayDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CNCBI01Header header = new CNCBI01Header();
            header.action = "DLCIDSTT";
            writeCommonHeader(xmlWriter, header);
//            <clientID></clientID><!--客户流水号char(20)-->
            xmlWriter.writeElementString("clientID", reqQryForeignCurrSingle.askNumber);
//            <type></type><!--
            xmlWriter.writeElementString("type", "");
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if (bankResult[0].length() == 0) {
                StringBuilder tmpBkMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpBkMsg);
                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                    String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                    if (tmpStatus != null && tmpStatus.equals("AAAAAAA")) {
                        if (tmpBkReader.read("/stream/list")) {
                            if (tmpBkReader.readedXElement.getAttrValue("name").equals("userDataList")) {
                                detail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                                detail.respCode = "0";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleText("row/statusText");
                                detail.payInfoCode = tmpBkReader.readedXElement.getSingleText("row/status");
                                detail.transState = "stt+" + tmpBkReader.readedXElement.getSingleText("row/stt");
                                if ("AAAAAAB".equals(detail.payInfoCode) || "AAAAAAC".equals(detail.payInfoCode)) {
                                    detail.transState = detail.payInfoCode;
                                }
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "未找到userDataList的list";
                            }
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "未找到/stream/list";
                        }
                    } else if (tmpStatus != null && tmpStatus.length() > 0) {
                        if ("ED12001".equals(tmpStatus) || "ED12002".equals(tmpStatus)) {//系统登录中,请稍候再试.
                            detail.respCode = "-1";
                        } else {
                            detail.respCode = "0";
                        }
                        detail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                        detail.payInfoCode = tmpStatus;
                        detail.respInfo = tmpStatusText;
                        detail.transState = tmpStatus + "+" + tmpStatusText;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpStatusText;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpBkMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeRetDetail(detail);
        return retAts;
    }
}
